/* 
 * Copyright 2013 Imanuel Rohlfing, Dennis Wittkötter

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package management.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.validation.constraints.NotNull;


/**
 * A room object which can be persisted to a database.
 * The room has an unique id, a name and a building, where it is placed.
 * Furthermore the room has a collection of persons which are granted to access
 * the room.
 * @author Dennis Wittkötter
 */
@Entity
@ManagedBean(name="room")
@RequestScoped
public class Room implements Serializable{

    private static final long serialVersionUID = 1L;
    
    /**
     * An unique id. It is generated automatically.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    
    /**
     * The name of the room.
     */
    @NotNull
    private String name;
    
    /**
     * The building where the room is placed.
     */
    @NotNull
    private String building;
    
    /**
     * A collection of persons which are granted to access the room.
     */
    @ManyToMany
    private List<Person> persons;
    
    /**
     * Default constructor to create a room object.
     */
    public Room() {
        this.persons = new ArrayList<>();
    }
    
    /**
     * Creates a room with the commited name.
     * @param roomName 
     */
    public Room(String roomName) {
        this.name = roomName;
        
        this.persons = new ArrayList<>();
    }
    
    /**
     * Provides the room id.
     * @return  room id
     */
    public int getId() {
        return id;
    }

    /**
     * Sets the name of the room.
     * @param name  room name
     */
    public void setName(String name) {
        this.name = name;
    }
    
    /**
     * Provides the name of the room.
     * @return  room name
     */
    public String getName() {
        return name;
    }

    /**
     * Set the name of the building of the room.
     * @param buildingName  name or label of the building
     */
    public void setBuilding(String buildingName) {
        this.building = buildingName;
    }
    
    /**
     * Provides the name of the building of the room.
     * @return  name or label of the building
     */
    public String getBuilding() {
        return building;
    }

    /**
     * Adds a persons which is linked to the room.
     * @param person the person to set
     */
    public void setPerson(Person person) {
        this.persons.add(person);
    }    
    
     /**
     * Removes a persons which is linked to the room.
     * @param person the person to remove
     */
    public void removePerson(Person person) {
        this.persons.remove(person);
    }     
    
    /**
     * Sets a list of persons which are linked to the room.
     * @param persons the persons to set
     */
    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }
    
    /**
     * Provides a list of persons which are linked to the room.
     * @return the persons
     */
    public List<Person> getPersons() {
        return persons;
    }

    @Override
    public String toString() {
        return this.name;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 59 * hash + this.id;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Room other = (Room) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }


    
    
}
